<template>
   <PanelSplitTwo bottomToolbarHeight="0" :num="2">
    <template #top>
      <SetUserSelect
        :key="keys"
        class="queryForm"
        :formData="orderSelect"
        v-model:queryParams="paginationParams"
        @selectFunc="selectFunc"
      ></SetUserSelect>
    </template>
     <template #centerToolbar>
        <div class="vex_flex">
          <el-row :gutter="20" style="margin-bottom: 10px">
            <el-col :span="1.5">
              <el-button @click="handleQuery">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="handleQuery(1)">查看</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="delFun">删除</el-button>
            </el-col>
          </el-row>
        <VxeTableGrid
          style="flex: 1;"
          :tableColumn="tableColumn"
          :tableLoading="tableLoading"
          :dataList="dataList"
          :showPagination="true"
          :paginationParams="paginationParams"
          @getList="setQueryParams"
          @selectedChange="selectedChange"
        >
        </VxeTableGrid>
      </div>
     </template>
   </PanelSplitTwo>
</template>
<script setup name="DepartmentList" lang="jsx">
import { useRouter } from 'vue-router'
import { drugList, deldrug  } from "@/api/basicManagement/varietyMana";
import { drugTypeList  } from "@/api/basicManagement/categoryMana";


import { ElMessageBox } from 'element-plus'
const { proxy } = getCurrentInstance();
const { variety_mana_owner, variety_mana_status } = proxy.useDict("variety_mana_owner", "variety_mana_status");

import datas from './data.js'

const keys = ref(0)
const Id = ref('')
const { paginationParams, orderSelect }  = datas
const tableColumn = ref([
  { type: "checkbox", width: 60, fixed: null },
  { field: "name", title: "名称" },
  { field: "number", title: "编码" },
  { field: "typeId", title: "品类",
  slots: { default: ({ row }, h) => {
    return (orderSelect[3].selectData?.filter(item => item.id == row.typeId)).name
  }} },
  { field: "owner", title: "所属",
  slots: { default: ({ row }, h) => {
    return [<dict-tag options={ variety_mana_owner.value } value={ row.owner }></dict-tag>]
  }} },
  { field: "status", title: "状态",
  slots: { default: ({ row }, h) => {
    return [<dict-tag options={ variety_mana_status.value } value={ row.status }></dict-tag>]
  }} },
])

watch(
  variety_mana_owner,
  (newV, oldV) => {
    orderSelect[4].selectData = variety_mana_owner.value;
    keys.value = new Date().getTime();
  }
);

watch(
  variety_mana_status,
  (newV, oldV) => {
    orderSelect[2].selectData = variety_mana_status.value;
    keys.value = new Date().getTime();
  }
);


const router= useRouter ()



const pageSize = ref(10);
let tableLoading = ref(false)
const dataList = ref([
])

// 切换分页
  function setQueryParams(val) {
    paginationParams.value.pageNum = val.pageNum;
   paginationParams.value.pageSize = val.pageSize;
    initDate()
  }
  // 选择表格
  let selectedList = ref([])
  function selectedChange(val) {
    Id.value = val.currentItem.id
    selectedList.value = val.selectedList
    // this.form.customerId = val.currentItem.customerId;
  }
/** 搜索按钮操作 */
function handleQuery(type) {
  router.push({
    path: '/basicInformation/addVarieties',
    query: {
      id: type == 1? Id.value : ''
    }
  })
}
const selectFunc = () => {
  paginationParams.value.pageNum = 1
  initDate()
}
const delFun = () => {
  ElMessageBox.confirm('是否确认删除?')
    .then(() => {
      deldrug(Id.value).then(() => {
        proxy.$modal.msgSuccess(`删除成功`);
        initDate()
      })
    })
    .catch(() => {
      // catch error
    })
}
const initDate = () => {
  drugList(paginationParams.value).then((res) => {
    paginationParams.value.total = res.total
    dataList.value = res.rows
  })
}

const drugTypeListFun = () => {
  drugTypeList({
    pageNum: 1,
    pageSize: 100000,
  }).then((res) => {
    res.rows.length && res.rows.forEach((item) => {
      item.value = item.id
      item.label = item.name
    })
    orderSelect[3].selectData = res.rows
    keys.value = new Date().getTime();
    initDate()
  })
}
drugTypeListFun()
</script>
<style scoped>
  .app-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 85px);
    overflow: hidden;
  }
  .vex_flex {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
  }
  .flexmin {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
</style>